package com.lucky_byte.pay.jar.yins;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.google.gson.JsonObject;
import com.lucky_byte.pay.jar.Packet;

/**
 * 银盛支付报文 v3.0
 */
public class PacketY30 extends Packet
{
	private static final Logger logger = LogManager.getLogger();

	private Map<String, String> fset;

	public PacketY30() {
		fset = new HashMap<>();
	}

	@Override
	public boolean has(String name) {
		return fset.containsKey(name);
	}

	@Override
	public String get(String name) {
		return fset.get(name);
	}

	@Override
	public boolean set(String name, String value) {
		if (name == null) {
			logger.error("set() 参数无效");
			return false;
		}
		if (value == null) {
			fset.remove(name);
			return false;
		}
		fset.put(name, value);
		return true;
	}

	@Override
	public int count() {
		return fset.size();
	}

	@Override
	public void remove(String name) {
		fset.remove(name);
	}

	@Override
	public void clear() {
		fset.clear();
	}

	@Override
	public List<String> keySet() {
		List<String> keyset = new ArrayList<>();
		for (String key : fset.keySet()) {
			keyset.add(key);
		}
		return keyset;
	}

	@Override
	public Packet clone() {
		PacketY30 clone = new PacketY30();
		for (String key : this.fset.keySet()) {
			clone.fset.put(key, this.fset.get(key));
		}
		return clone;
	}

	@Override
	public JsonObject toJson() {
		JsonObject json_object = new JsonObject();
		for (String key : fset.keySet()) {
			json_object.addProperty(key, fset.get(key));
		}
		return json_object;
	}

}
